<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\User;
use App\Models\Areas;
use App\Models\Configs;
use App\Models\Mails;
use App\Models\UserInfos;
use App\Models\Users;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\HRExport;
use App\Exports\SupplierExport;

class UserController extends Controller
{
    /**
     * 供应商页面
     */
    public function supplierIndex()
    {
        return view('admin.user.supplier');
    }

    /**
     * HR页面
     */
    public function hrIndex()
    {
        return view('admin.user.hr');
    }

    /**
     * 列表数据
     * @param Request $request
     * @return bool
     */
    public function List(Request $request)
    {
        try {
            $params = [
                'kwd' => (string)$request->get('kwd'),
                'type' => (int)$request->get('type'),
                'status' => (int)$request->get('status'),
                'balance' => $request->get('balance',''),
            ];
            $lists = Users::getList($params);
            foreach ($lists as $key => $val){
                $lists[$key]['balance'] = floatval($val['balance']);
            }
            return $this->writeJson(0, $lists);
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

    /**
     * HR查看
     */
    public function hrEdit(Request $request)
    {
        try {
            $id = (int)$request->get('id', 0);
            if (empty($id)) {
                return $this->writeJson(1, null, '参数错误');
            }
            $info = Users::findOrFail($id);
            $info['sex'] = $info['sex'] == 1 ? '男' : '女';
            $info['type'] = $info['type'] == 1 ? 'HR' : '供应商';
            $info['area'] = Areas::where('id',$info['area'])->value('name');
            $info['user_info'] = UserInfos::getOneList(['user_id'=>$id]);
            $info['reason'] = '';//19-9-20 by z
            $info = json_encode($info);
            return view('admin.user.detail', compact('info'));
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

    /**
     * 供应商查看
     */
    public function supplierEdit(Request $request)
    {
        try {
            $id = (int)$request->get('id', 0);
            if (empty($id)) {
                return $this->writeJson(1, null, '参数错误');
            }
            $info = Users::findOrFail($id);
            $info['sex'] = $info['sex'] == 1 ? '男' : '女';
            $info['type'] = $info['type'] == 1 ? 'HR' : '供应商';
            $info['area'] = Areas::where('id',$info['area'])->value('name');
            $info['user_info'] = UserInfos::getOneList(['user_id'=>$id]);
            $info['reason'] = '';//19-9-20 by z
            $info = json_encode($info);
            return view('admin.user.detail', compact('info'));
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

    /**
     * 更新
     * @param User $request
     * @return bool
     */
    public function update(User $request)
    {
        try {
            $data = $request->all();
            $map = [
                'integral' => $data['integral'],
                'balance' => $data['balance'],
                'bond' => $data['bond'],
                'profit' => $data['profit'],
                'contract' => $data['contract'],
                'cash' => $data['cash'],
                'member_fee' => $data['member_fee'],
                'status' => $data['status'],
            ];
            if(!empty($data['member_fee_at'])){
                $map['member_fee_at'] = $data['member_fee_at'];
            }
            if ($data['id']) {
                $info = Users::findOrFail($data['id']);
                //19-9-20 by z 除正常状态意外其余需给用户发送站内信告知原因 start
                if($map['status'] != 1){
                    $reason = $data['reason'];
                    if(empty($reason)){
                        return $this->writeJson(1, null, '请填写相关状态原因！');
                    }
                    if($data['status'] == 2 || $data['status'] == 3){
                        $title = '账户审核通知';
                    }elseif ($data['status'] == 4){
                        $title = '账户锁定通知';
                    }
                    Mails::sendMails($title,$data['reason'],$data['id'],1);
                }
                //end

                $info->update($map);
                return $this->writeJson(0, null, '修改用户信息成功');
            } else {
                unset($data['id']);
                $data['password'] = $data['password'] ? bcrypt($data['password']) : bcrypt('123456');
                $data['register_ip'] = $request->ip();
                $data['login_at'] = Carbon::now();
                $data['login_ip'] = $request->ip();
                $base = Configs::getConfig('base');
                $data['avatar'] = $base['default_avatar'];
                Users::create($data);
                return $this->writeJson(0, null, '新增用户信息成功');
            }
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

    /**
     * 拒绝
     * @param Request $request
     * @return bool
     */
    public function lock(Request $request)
    {
        try {
            $id = (int)$request->get('id', 0);
            if (empty($id)) {
                return $this->writeJson(1, null, '参数错误');
            }
            $info = Users::findOrFail($id);
            $info->update(['status' => Users::STATUS_3]);
            return $this->writeJson(0);
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

    /**
     * 激活
     * @param Request $request
     * @return bool
     */
    public function active(Request $request)
    {
        try {
            $id = (int)$request->get('id', 0);
            if (empty($id)) {
                return $this->writeJson(1, null, '参数错误');
            }
            $info = Users::findOrFail($id);
            $info->update(['status' => Users::STATUS_1]);
            return $this->writeJson(0);
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

    /**
     * 锁定
     * @param Request $request
     * @return bool
     */
    public function close(Request $request)
    {
        try {
            $id = (int)$request->get('id', 0);
            $reason = (string)$request->get('reason', '');
            if (empty($id) || empty($reason)) {
                return $this->writeJson(1, null, '参数错误');
            }
            $info = Users::findOrFail($id);
            $info->update(['status' => Users::STATUS_4]);

            //19-9-23 by z 锁定发送站内信 start
            Mails::sendMails('账户锁定通知',$reason,$id,1);
            //end

            return $this->writeJson(0);
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

    /**
     *  单个/多个删除
     * @param Request $request
     * @return bool
     */
    public function destroy(Request $request)
    {
        try {
            $id = $request->get('id');
            if (empty($id)) {
                return $this->writeJson(1, null, '参数错误');
            }
            if(!is_array($id)){
                $id=[$id];
            }
            //修改软删除 19-7-31
            $data['status'] = -1;
            Users::whereIn('id',$id)->update($data);
            return $this->writeJson(0);
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

    /**
     *  HRExcel导出
     */
    public function hrExports(Request $request)
    {
        $ids = $request->get('ids');
        $export = new HRExport($ids);
        //这里可以传参到服务层
        return Excel::download($export, 'HR用户表'.date("Y-m-d H:i:s").'.xlsx');
    }

    /**
     *  供应商Excel导出
     */
    public function supplierExports(Request $request)
    {
        $ids = $request->get('ids');
        $export = new SupplierExport($ids);
        //这里可以传参到服务层
        return Excel::download($export, '供应商用户表'.date("Y-m-d H:i:s").'.xlsx');
    }

}
